/**
 * 子渠道管理初始化
 */
var Channel = {
    id: "ChannelTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Channel.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '渠道名称', field: 'channelName', visible: true, align: 'center', valign: 'middle'},
            {title: '激活量', field: 'activeCount', visible: true, align: 'center', valign: 'middle'},
            {title: '注册量', field: 'registerCount', visible: true, align: 'center', valign: 'middle'},
            {title: '申请量', field: 'requestCount', visible: true, align: 'center', valign: 'middle'},
            {title: '成交量', field: 'bargainCount', visible: true, align: 'center', valign: 'middle'},
            {title: '成交额', field: 'bargainAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Channel.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Channel.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加子渠道
 */
Channel.openAddChannel = function () {
    var index = layer.open({
        type: 2,
        title: '添加子渠道',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channel/channel_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看子渠道详情
 */
Channel.openChannelDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '子渠道详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channel/channel_update/' + Channel.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除子渠道
 */
Channel.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/channel/delete", function (data) {
            Feng.success("删除成功!");
            Channel.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("channelId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询子渠道列表
 */
Channel.search = function () {
    var queryData = {};
    queryData['beginDate'] = $("#beginDate").val();
    queryData['endDate'] = $("#endDate").val();
    Channel.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Channel.initColumn();
    var table = new BSTable(Channel.id, "/countChannel/list", defaultColunms);
    table.setPaginationType("client");
    Channel.table = table.init();
    function getValue(url) {
        //首先获取地址
        var url = url || window.location.href;
        //获取传值
        var arr = url.split("?");
        //判断是否有传值
        if (arr.length == 1) {
            return null;
        }
        //获取get传值的个数
        var value_arr = arr[1].split("&");
        //循环生成返回的对象
        var obj = {};
        for (var i = 0; i < value_arr.length; i++) {
            var key_val = value_arr[i].split("=");
            obj[key_val[0]] = key_val[1];
        }
        return obj;
    }
    var qudao = getValue()
    if (qudao != null && qudao.channelId > 0) {
        var queryData = {};
        queryData['channelId'] = qudao.channelId;
        Channel.table.refresh({query: queryData});
    }
});

Channel.list = function () {
        location.href = Feng.ctxPath + "/countChannelSuper";

};